package com.nanjia.boot.invoicetypeconfig.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.invoicetypeconfig.model.form.InvoiceTypeForm;
import com.nanjia.boot.invoicetypeconfig.model.query.InvoiceTypeQuery;
import com.nanjia.boot.invoicetypeconfig.model.vo.InvoiceTypeVO;
import com.nanjia.boot.invoicetypeconfig.service.InvoiceTypeService;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 发票类型前端控制层
 *
 * @author zhouyang
 * @since 2025-06-13 10:11
 */
@Tag(name = "发票类型接口")
@RestController
@RequestMapping("/api/v1/invoice-type")
@RequiredArgsConstructor
public class InvoiceTypeController  {

    private final InvoiceTypeService invoiceTypeService;

    @Operation(summary = "发票类型分页列表")
    @GetMapping("/page")
    // @PreAuthorize("@ss.hasPerm('invoiceTypeConfig:invoice-type:query')")
    public PageResult<InvoiceTypeVO> getInvoiceTypePage(InvoiceTypeQuery queryParams ) {
        IPage<InvoiceTypeVO> result = invoiceTypeService.getInvoiceTypePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增发票类型")
    @PostMapping
    // @PreAuthorize("@ss.hasPerm('invoiceTypeConfig:invoice-type:add')")
    public Result<Void> saveInvoiceType(@RequestBody @Valid InvoiceTypeForm formData , HttpServletRequest request) {
        boolean result = invoiceTypeService.saveInvoiceType(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取发票类型表单数据")
    @GetMapping("/{id}/form")
    // @PreAuthorize("@ss.hasPerm('invoiceTypeConfig:invoice-type:edit')")
    public Result<InvoiceTypeForm> getInvoiceTypeForm(
        @Parameter(description = "发票类型ID") @PathVariable Long id
    ) {
        InvoiceTypeForm formData = invoiceTypeService.getInvoiceTypeFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改发票类型")
    @PutMapping(value = "/{id}")
    // @PreAuthorize("@ss.hasPerm('invoiceTypeConfig:invoice-type:edit')")
    public Result<Void> updateInvoiceType(
            @Parameter(description = "发票类型ID") @PathVariable Long id,
            @RequestBody @Validated InvoiceTypeForm formData,HttpServletRequest request
    ) {
        boolean result = invoiceTypeService.updateInvoiceType(id, formData,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除发票类型")
    @DeleteMapping("/{ids}")
    // @PreAuthorize("@ss.hasPerm('invoiceTypeConfig:invoice-type:delete')")
    public Result<Void> deleteInvoiceTypes(
        @Parameter(description = "发票类型ID，多个以英文逗号(,)分割") @PathVariable String ids,HttpServletRequest request
    ) {
        boolean result = invoiceTypeService.deleteInvoiceTypes(ids,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }
}
